(function () {

    let DetectDropdown = function (item, con, isOutSide = true, type) {
        let handles = {};
        if (item) {
            var containers = [];
            var id = item && item.getAttribute("id");
            function createHandle() {
                var ret = function (e) {
                    var self = this;
                    var ret = false;
                    if (containers) {
                        ret = containers.some(function (container) {
                            return container.contains(e.target) || e.target === container;
                        });
                    }
                    if (!ret) {
                        ele.auiRemoveOnceClass("open");
                        document.removeEventListener("click", self);
                    }
                }
                return ret;
            }
            var forelement = item.getAttribute("for");
            var ele = document.getElementById(forelement);        
            containers = con.concat([
                item,
                ele
            ]);
            if (!type) {
                var clickhandle = createHandle();
                ele.addEventListener("click", function (e) {
                    e.stopPropagation();
                })
                item.addEventListener("click", function () {
                    if (!ele.classList.contains("open")) {
                        ele.auiAddOnceClass("open");
                        if (isOutSide) {
                            document.addEventListener("click", clickhandle);
                        }
                    } else {
                        ele.auiRemoveOnceClass("open");
                        document.removeEventListener("click", clickhandle);
                    }
                });
            } else {
                if (type === "hover") {
                    function sdsdsdsdsd() {
                        return function(e) {
                            var self = this;
                            var ret = containers.some(function (container) {
                                return container.contains(e.target) || e.target === container;
                            });
                            if (!ret) {
                                ele.auiRemoveOnceClass("open");
                                document.removeEventListener("mousemove", self);
                            }
                        }
                    }
                    let detecthandle = sdsdsdsdsd();
                    ele.addEventListener("mouseover", function () {
                        document.removeEventListener("mousemove", detecthandle);
                    });
                    ele.addEventListener("mouseleave", function () {
                        ele.auiRemoveOnceClass("open");
                    });
                    item.addEventListener("mouseenter", function () {
                        ele.auiAddOnceClass("open");
                        document.removeEventListener("mousemove", detecthandle);
                    });
                    item.addEventListener("mouseleave", function (e) {
                        setTimeout(function () {
                            document.removeEventListener("mousemove", detecthandle);
                        }, 170);
                    });
                }
            }
        }
    }

    let template = function (self) {
        return self.innerHTML;
    };

    document.auiDefineElement(
        'aux-dropdown',
        {
            template,
            attached(self) {
                let method = self.auiGetAttribute("type", undefined);
                let isOutSide = (self.auiGetAttribute("outside") == null ? false : true);
                new DetectDropdown(self, [], isOutSide, method);
            }
        }
    );
})();
